﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InHD.model;
using System.Data;
using InHD.utilities;

namespace InHD.dataaccess
{
    class RegisterPrintingRepository
    {
        #region Fields

        readonly List<RegisterPrinting> _registerPrinting;
        //For DB Utils
        private DBUltils _db;
        string[] _paramName;
        string[] _value;
        DBUltils.ParamType[] _paramType;

        #endregion // Fields
                
        #region Constructor

        /// <summary>
        /// Creates a new repository of 
        /// </summary>
        public RegisterPrintingRepository()
        {
            _registerPrinting = new List<RegisterPrinting>();
            this._db = new DBUltils();
            if (!_db.connectDB(Constants.Security.DefaultDBUser, Constants.Security.DefaultDBPassword))
                throw new ArgumentNullException(Constants.Messages.CannotConnectDB);
        }

        #endregion // Constructor

        #region Public Interface

        /// <summary>
        /// Raised when a Transaction is placed into the repository.
        /// </summary>
        public event EventHandler<RegisterPrintingAddedEventArgs> RegisterPrintingAdded;

        public static bool AddNewRegister(RegisterPrinting _registerPrinting)
        {
            string storeProName = "AddReg";
            List<string> paramName = new List<string>();
            paramName.Add("@regUNK");
            paramName.Add("@modified");
            paramName.Add("@modifyBy");
            paramName.Add("@regDate");
            paramName.Add("@invoiceUNK");
            paramName.Add("@status");
            paramName.Add("@receiverUNK");
            paramName.Add("@startDate");
            paramName.Add("@expireDate");
            paramName.Add("@number");
         
            List<object> paramValue = new List<object>();
            paramValue.Add("reg" + Guid.NewGuid().ToString());
            paramValue.Add(DateTime.Now);
            paramValue.Add(1); ////hardCode
            paramValue.Add(DateTime.Now);
            paramValue.Add(_registerPrinting.InvoiceUNK);
            paramValue.Add("new"); ////hardCode
            paramValue.Add(_registerPrinting.ReceiverUNK);
            paramValue.Add(_registerPrinting.StartDate);
            paramValue.Add(_registerPrinting.ExpireDate);
            paramValue.Add(_registerPrinting.Number);

            try
            {
                int newID = DBUltils.CRUDQuery(storeProName, paramName, paramValue);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Returns a shallow-copied list of all Transactions in the repository.
        /// </summary>
        public List<RegisterPrinting> GetRegisterPrinting()
        {
            return new List<RegisterPrinting>(_registerPrinting);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="invoiceTplUNK"></param>
        /// <returns>False if not existing</returns>
        public DataRow CheckRegisterExistenceByInvoiceTpl(string invoiceTplUNK)
        {
            try
            {
                DataSet ds;
                if (_db == null)
                    return null;
                if (string.IsNullOrEmpty(invoiceTplUNK))
                    return null;
                _paramName = new string[] { "@invoiceTplUNK" };
                _paramType = new DBUltils.ParamType[] { DBUltils.ParamType.Str };
                _value = new string[] { invoiceTplUNK };

                ds = _db.queryDB("[GetRegByInvoiceTemplate]", _paramName, _paramType, _value, DBUltils.QueryType.Select);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    return ds.Tables[0].Rows[0];
                }
            }
            catch (Exception e)
            {
                Logger.RecordMessage(e, MessageType.Error);
                return null;
            }

            return null;
        }

        #endregion // Public Interface
    }
}
